package com.kedou.sdk.ui.assist;

import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.text.Html;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.CheckBox;
import android.widget.ImageView;
import android.widget.TextView;

import com.kedou.sdk.params.bean.Goods;
import com.kedou.sdk.params.bean.PayChannel;
import com.kedou.sdk.ui.activity.PlatFormCoinActivity;
import com.kedou.sdk.utils.ResUtils;
import com.squareup.picasso.Picasso;

import java.util.ArrayList;
import java.util.List;

public class PayChannelListAdapter extends BaseAdapter {

    private final List<PayChannel> mChannels = new ArrayList<>();
    private LayoutInflater inflater;
    private boolean isFirstLoadView;
    private int lastSelectedIndex;
    private Goods mGoods;
    private float ptbFloat;

    public PayChannelListAdapter(Goods goods,float ptb){
        mGoods = goods;
        this.ptbFloat = ptb;
    }


    public void setAdapterData(List<PayChannel> data) {
        this.mChannels.clear();
        this.mChannels.addAll(data);
        this.isFirstLoadView = true;
    }

    public void setLastSelectedIndex(int position){
        this.lastSelectedIndex = position;
    }

    @Override
    public int getCount() {
        return mChannels.size();
    }

    @Override
    public PayChannel getItem(int position) {
        return mChannels.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, final ViewGroup parent) {
        final Context context = parent.getContext();
        ViewHolder viewHolder;
        if (convertView == null) {
            viewHolder = new ViewHolder();

            if(inflater == null){
                inflater = LayoutInflater.from(context);
            }

            final int layoutResId = ResUtils.getLayoutIdentifier(context, "hnssn_item_payment_channel");
            final int iconResId = ResUtils.getViewIdentifier(context, "hnssn_item_payment_icon");
            final int titleResId = ResUtils.getViewIdentifier(context, "hnssn_item_payment_title");
            final int descResId = ResUtils.getViewIdentifier(context, "hnssn_item_payment_description");
            final int checkResId = ResUtils.getViewIdentifier(context, "hnssn_item_payment_checkbox");
            final int chargeResId = ResUtils.getViewIdentifier(context, "hnssn_item_payment_description2");

            convertView = inflater.inflate(layoutResId, parent, false);
            viewHolder.title = convertView.findViewById(titleResId);
            viewHolder.desc = convertView.findViewById(descResId);
            viewHolder.icon = convertView.findViewById(iconResId);
            viewHolder.checked = convertView.findViewById(checkResId);
            viewHolder.charge = convertView.findViewById(chargeResId);


            convertView.setTag(viewHolder);
        } else {
            viewHolder = (ViewHolder) convertView.getTag();
        }

        PayChannel channel = getItem(position);
        if(channel != null){
            viewHolder.title.setText(channel.getTitle());
            viewHolder.charge.setText(Html.fromHtml("<u>"+"立即充值"+"</u>"));
            viewHolder.charge.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
//                    if(XmlUtils.showThirdLogin()){
//                        ThirdLoginUtil.getInstance().startPay(parent.getContext());
//                    }else {
                        Intent intent = new Intent(parent.getContext(), PlatFormCoinActivity.class);
                        intent.putExtra("goods",mGoods);
                        parent.getContext().startActivity(intent);
//                    }
                }
            });
            Picasso.with(context).load(channel.getIconUrl()).into(viewHolder.icon);
            if(channel.getPayType()== 200){
                viewHolder.charge.setVisibility(View.VISIBLE);
                viewHolder.desc.setTextColor(Color.parseColor("#F03047"));
                viewHolder.desc.setText("当前剩余："+ptbFloat);
            }else {
                viewHolder.charge.setVisibility(View.GONE);
                viewHolder.desc.setTextColor(Color.parseColor("#989FA5"));
                viewHolder.desc.setText(channel.getDescription());

            }
        }

        viewHolder.checked.setChecked(position == lastSelectedIndex);
        if(isFirstLoadView){
            isFirstLoadView = false;
        }

        return convertView;
    }

    public static class ViewHolder {
        public TextView title;
        public TextView desc;
        public ImageView icon;
        public CheckBox checked;
        public TextView charge;
    }

}
